import { createRouter, createWebHashHistory } from 'vue-router'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      components:{
        header:  () => import('../views/Header.vue'),
        default:  () => import('../views/HomeView.vue')
      },
      // redirect: "/home",
      // alias: "/home",
    },
    {
      path: '/news',
      name: 'news',
      components: {
        header: () => import('../views/Header.vue'),
        default: () => import('../views/News/index.vue')
      },
      children:[{
        path:"detail/:id/:abc",
        name:"detail",
        component: () => import('../views/News/Detail.vue')
      }],
      meta:{ msg:"hello", title:"hahahha" }
    },
    {
      path:"/goods",
      name:"goods",
      components: {
        header: () => import('../views/Header.vue'),
        default: () => import('../views/Goods/index.vue')
      },
      // beforeEnter(to, from){
      //   // 单个路由守卫：只针对当前路由
      //   console.log(to);
      //   if(true){
      //     return '/login?from='+to.path
      //   }
      // }
    },
    {
      path:"/detail/:id",
      name:"gDetail",
      component: ()=> import('../views/Goods/Detail.vue'),
      props: true
    },
    {
      path:"/login",
      name:"login",
      component: ()=> import('../views/Login.vue')
    }
  ]
})
// 全局的路由守卫：任何路由切换之前，都会触发该函数
// 形参1：下一个路由（去哪）
// 形参2：上一个路由（从哪来）
// router.beforeEach((a, b)=>{
//   // console.log(a);
//   // console.log(b);
//   if( localStorage.getItem("isLogin") !== "ok" && a.path !== "/login" ){
//     return '/login';
//   }
// });

export default router
